﻿
//    function getCrtlPrefix()
//            {
//	            var prefix;
//            		
//	            var	objCrtlPrefix = document.getElementById("ctrlUserPrefix");
//            		
//	            if (objCrtlPrefix)
//		            prefix = objCrtlPrefix.value;
//            			
//            	//alert(prefix);
//	            return prefix;
//            }

var orgDateMessage = "";
function CheckDate(sender, args) {
    var prefix = sender.id.toString();
    //alert(prefix);
    var arrprefix = prefix.split("_");
    var strprefix = "";
    var strsourceid = "";

    var arrlen = arrprefix.length - 1;
    for (i = 0; i < arrlen; i++) {
        strprefix = strprefix + arrprefix[i].toString() + "_";
    }
    strsourceid = arrprefix[arrlen];

    var cvalD = document.getElementById(strprefix + 'ddlDay');
    var cvalM = document.getElementById(strprefix + 'ddlMonth');
    var cvalY = document.getElementById(strprefix + 'ddlYear');
    var allowgtoday = document.getElementById(strprefix + 'txtAllowGreaterThanToday');
    var istravelinsurance = document.getElementById(strprefix + 'txtIsTravelInsurance');
    var isnotlessthantoday = document.getElementById(strprefix + 'txtIsNotLessThanToday');
    
    var csvDate = document.getElementById(prefix);



    //if(orgDateMessage.length == 0) orgDateMessage = csvDate.errormessage;

    //        if(csvDate.textContent)
    //            csvDate.textContent += "Date of Birth.";
    //        else
    //            csvDate.innerText +="Date of Birth.";

    //alert(csvDate);
    //csvDate.innerText = "HELLO, YOU DATE IS NOT VALID!";
    // *******Commment - NO NEED ********************//

    //alert(csvDate.errormessage);
    //ValidatorUpdateDisplay(csvDate);
    // ******** NO NEED *****//
    var isvalid = true;
    var isDvalid = true;
    var isMvalid = true;
    var isYvalid = true;

    var y = parseInt(cvalY.value);
    var m = parseInt(cvalM.value);
    var d = parseInt(cvalD.value);
    if (cvalD.value == -1 && cvalM.value == -1 && cvalY.value == -1) {
        //isvalid = false;
        //isDvalid = false;
        //isMvalid =false;
        //isYvalid = false;
    }
    else if (cvalD.value == -1 || cvalM.value == -1 || cvalY.value == -1) {
        if (csvDate.textContent) {
            //alert('textContent');
            csvDate.textContent = "";
        }
        else {
            csvDate.innerText = "";
        }
        //isvalid = false;
        if (cvalD.value == -1) isDvalid = false;
        if (cvalM.value == -1) isMvalid = false;
        if (cvalY.value == -1) isYvalid = false;
    }
    else {
        isvalid = true;
        isDvalid = true;
        isMvalid = true;
        isYvalid = true;

        if (y % 4 == 0 || y % 2000 == 0 || y % 400 == 0) {
            if (m == 2 && d > 29) {
                if (csvDate.textContent) {
                    //alert('textContent');
                    //csvDate.textContent = "Maximum days for this month are 29 days.";
                }
                else {
                    //csvDate.innerText ="Maximum days for this month are 29 days.";
                }
                //csvDate.errormessage="Maximum days for this month are 30 days.";
                isMvalid = false;
                isDvalid = false;
                isvalid = false;
            }
        }
        else if (m == 2) {
            if (d > 28) {
                //csvDate.innerText ="Maximun day is 28.";
                if (csvDate.textContent) {
                    //alert('textContent');
                    //csvDate.textContent = "Maximum days for this month are 28 days.";
                }
                else {
                    //alert('innerText');
                    //csvDate.innerText = "Maximum days for this month are 28 days.";
                }
                //csvDate.errormessage = "Maximum days for this month are 28 days.";
                isDvalid = false;
                isMvalid = false;
                isvalid = false;
            }
        }

        if (m == 4 || m == 6 || m == 9 || m == 11) {
            if (d > 30) {
                //csvDate.innerText ="Maximun day is 30.";
                if (csvDate.textContent) {
                    //csvDate.textContent = "Maximum days for this month are 30 days.";
                }
                else {
                    //csvDate.innerText = "Maximum days for this month are 30 days.";
                }
                //csvDate.errormessage =  "Maximum days for this month are 30 days.";
                isDvalid = false;
                isMvalid = false;
                isvalid = false;
            }
        }

        valallowgtoday = parseInt(allowgtoday.value);
        
        if (valallowgtoday == 0 && isvalid == true) {
            var now = new Date();
            var nowM = now.getMonth() + 1;
            var nowY = now.getYear();
            var nowD = now.getDate();
            if (nowY < 1900) { nowY += 1900; }

            if (y == nowY) {
                if (m >= nowM) {
                    if (d > nowD && nowM == m) {
                        //csvDate.innerText = "Over the today date. [DAY]";
                        if (csvDate.textContent) {
                            //csvDate.textContent = "Over the today date. [DAY].";
                        }
                        else {
                            //csvDate.innerText = "Over the today date. [DAY].";
                        }
                        //csvDate.errormessage = "Invalid Date. Over the today date. [DAY].";
                        isDvalid = false;
                        isvalid = false;
                    }
                    else if (m > nowM) {
                        if (csvDate.textContent) {
                            //csvDate.textContent = "Over the today date. [MONTH].";
                        }
                        else {
                            //csvDate.innerText = "Over the today date. [MONTH].";
                        }
                        //csvDate.errormessage = "Invalid Date. Over the today date. [MONTH].";
                        isMvalid = false;
                        isvalid = false;
                    }
                }
            }
            else if (y > nowY) {
                if (csvDate.textContent) {
                    //csvDate.textContent = "Over the today date. [YEAR].";
                }
                else {
                    //csvDate.innerText = "Over the today date. [YEAR].";
                }
                //csvDate.errormessage = "Invalid Date. Over the today date. [YEAR].";
                isYvalid = false;
                isvalid = false;
            }

        }

        valnotlessthantoday = parseInt(isnotlessthantoday.value);

        if (valnotlessthantoday == 1 && isvalid == true) {
            var now = new Date();
            var nowM = now.getMonth();
            var nowY = now.getFullYear();
            var nowD = now.getDate();
            var dateNow = new Date(nowY, nowM, nowD);
            var dateSel = new Date(y, m - 1, d);
            //alert('test ' + dateNow + ' ' + dateSel);
            
            if (dateSel < dateNow) {
                isYvalid = false;
                isvalid = false;
            }
            else {
                isYvalid = true;
                isvalid = true;
            }

        }

        valtravelinsurance = parseInt(istravelinsurance.value);

        //alert("valtravelinsurance");
            if (valtravelinsurance == 1 && isvalid == true) {
            //Compare start and end date :: Travel Insurance

            //var csvDate1 = document.getElementById('ctl00_ContentPlaceHolder1_usrS1TravelStartDate_csvDate');
            //var csvDate3 = document.getElementById('ctl00_ContentPlaceHolder1_usrS3TravelStartDate_csvDate');

            var now = new Date();
            var nowM = now.getMonth();
            var nowY = now.getFullYear();
            var nowD = now.getDate();
            var dateNow = new Date(nowY, nowM, nowD);

            var usrctrlNameStart = '';
            var usrctrlNameEnd = '';

            var ddlPolicyType = document.getElementById('ctl00_ContentPlaceHolder1_ddlPolicyType');
            //var ddlPolicyType = document.getElementById('<%=ddlPolicyType.ClientID%>');
            //alert(ddlPolicyType.value);
            
            //scenario 1
            if (ddlPolicyType.value == 'IND') {
                
                var ddlTripType = document.getElementById('<%=ddlTripType.ClientID%>');

                usrctrlNameStart = 'ctl00_ContentPlaceHolder1_usrS1TravelStartDate_';

                var dt1 = document.getElementById(usrctrlNameStart + 'ddlDay').value;
                var mon1 = document.getElementById(usrctrlNameStart + 'ddlMonth').value;
                var yr1 = document.getElementById(usrctrlNameStart + 'ddlYear').value;

                usrctrlNameEnd = 'ctl00_ContentPlaceHolder1_usrS1TravelEndDate_';

                var dt2 = document.getElementById(usrctrlNameEnd + 'ddlDay').value;
                var mon2 = document.getElementById(usrctrlNameEnd + 'ddlMonth').value;
                var yr2 = document.getElementById(usrctrlNameEnd + 'ddlYear').value;

                var date1 = new Date(yr1, mon1 - 1, dt1);
                var date2 = new Date(yr2, mon2 - 1, dt2);

               
                if (date2 < date1 && (dt2 != -1 && mon2 != -1 && yr2 != -1)) 
                {
                    isYvalid = false;
                    isvalid = false;
                    //csvDate1.textContent = 'Start date must be greater than End date';
                    
                }
                else if (date1 < dateNow)
                 {
                    isYvalid = false;
                    isvalid = false;

                } 
                else {
                    isYvalid = true;
                    isvalid = true;

                }

                //alert(isvalid);

            }
            else if (ddlPolicyType.value == 'FML')   //scenario 3
            {
               
                var ddlTripType = document.getElementById('<%=ddlTripType.ClientID%>');

                usrctrlNameStart = 'ctl00_ContentPlaceHolder1_usrS3TravelStartDate_';

                var s3dt1 = document.getElementById(usrctrlNameStart + 'ddlDay').value;
                var s3mon1 = document.getElementById(usrctrlNameStart + 'ddlMonth').value;
                var s3yr1 = document.getElementById(usrctrlNameStart + 'ddlYear').value;

                usrctrlNameEnd = 'ctl00_ContentPlaceHolder1_usrS3TravelEndDate_';
              
                var s3dt2 = document.getElementById(usrctrlNameEnd + 'ddlDay').value;
                var s3mon2 = document.getElementById(usrctrlNameEnd + 'ddlMonth').value;
                var s3yr2 = document.getElementById(usrctrlNameEnd + 'ddlYear').value;

                var s3date1 = new Date(s3yr1, s3mon1 - 1, s3dt1);
                var s3date2 = new Date(s3yr2, s3mon2 - 1, s3dt2);

              
                if (s3date2 < s3date1 && (s3dt2 != -1 && s3mon2 != -1 && s3yr2 != -1)) 
                {
                    isYvalid = false;
                    isvalid = false;
                    //csvDate3.textContent = 'Start date must be greater than End date';    

                }
                else if (s3date1 < dateNow) {
                    isYvalid = false;
                    isvalid = false;
                }
                else {
                    isYvalid = true;
                    isvalid = true;

                }

            }
            else if (ddlPolicyType.value == 'INS')   //scenario 5
            {
                var ddlTripType = document.getElementById('<%=ddlTripType.ClientID%>');

                usrctrlNameStart = 'ctl00_ContentPlaceHolder1_usrS5TravelStartDate_';

                var s5dt1 = document.getElementById(usrctrlNameStart + 'ddlDay').value;
                var s5mon1 = document.getElementById(usrctrlNameStart + 'ddlMonth').value;
                var s5yr1 = document.getElementById(usrctrlNameStart + 'ddlYear').value;

                usrctrlNameEnd = 'ctl00_ContentPlaceHolder1_usrS5TravelEndDate_';

                var s5dt2 = document.getElementById(usrctrlNameEnd + 'ddlDay').value;
                var s5mon2 = document.getElementById(usrctrlNameEnd + 'ddlMonth').value;
                var s5yr2 = document.getElementById(usrctrlNameEnd + 'ddlYear').value;

                var s5date1 = new Date(s5yr1, s5mon1 - 1, s5dt1);
                var s5date2 = new Date(s5yr2, s5mon2 - 1, s5dt2);

                //alert(date2);
                if (s5date2 < s5date1 && (s5dt2 != -1 && s5mon2 != -1 && s5yr2 != -1)) {
                    isYvalid = false;
                    isvalid = false;
                    //csvDate3.textContent = 'Start date must be greater than End date';    

                }
                else if (s5date1 < dateNow) {
                    isYvalid = false;
                    isvalid = false;
                }
                else {
                    isYvalid = true;
                    isvalid = true;

                }

            }

            if (isvalid) {
            
                var cS1valDstart = document.getElementById(usrctrlNameStart + 'ddlDay');
                var cS1valMstart = document.getElementById(usrctrlNameStart + 'ddlMonth');
                var cS1valYstart = document.getElementById(usrctrlNameStart + 'ddlYear');

                
                var cS1valDend = document.getElementById(usrctrlNameEnd + 'ddlDay');
                var cS1valMend = document.getElementById(usrctrlNameEnd + 'ddlMonth');
                var cS1valYend = document.getElementById(usrctrlNameEnd + 'ddlYear');

                //alert(cS1valDstart + ' ' + cS1valDend);
                if (cS1valDstart != null && cS1valDend != null) {
                    cS1valDstart.style.backgroundColor = 'transparent';
                    cS1valDstart.style.border = 'Solid 1px #0066FF';
                    cS1valDstart.style.fontWeight = '';
                    cS1valMstart.style.backgroundColor = '';
                    cS1valMstart.style.border = '';
                    cS1valMstart.style.fontWeight = '';
                    cS1valYstart.style.backgroundColor = '';
                    cS1valYstart.style.border = '';
                    cS1valYstart.style.fontWeight = '';

                    cS1valDend.style.backgroundColor = 'transparent';
                    cS1valDend.style.border = 'Solid 1px #0066FF';
                    cS1valDend.style.fontWeight = '';
                    cS1valMend.style.backgroundColor = '';
                    cS1valMend.style.border = '';
                    cS1valMend.style.fontWeight = '';
                    cS1valYend.style.backgroundColor = '';
                    cS1valYend.style.border = '';
                    cS1valYend.style.fontWeight = '';
                }

            }
            
            //csvDate1.textContent = '';
            //csvDate3.textContent = '';
        }

        if (isvalid == false) {
            cvalD.style.backgroundColor = '#fcc';
            cvalD.style.border = 'Solid 1px #f00';
            //cvalD.style.fontWeight = 'bold';

            cvalM.style.backgroundColor = '#fcc';
            cvalM.style.border = 'Solid 1px #f00';
            //cvalM.style.fontWeight = 'bold';

            cvalY.style.backgroundColor = '#fcc';
            cvalY.style.border = 'Solid 1px #f00';
            //cvalY.style.fontWeight = 'bold';

            /*
            if(!isDvalid)
            {
            //alert("D");
            cvalD.style.backgroundColor = '#fcc';
            cvalD.style.border = 'Solid 1px #f00';
            cvalD.style.fontWeight = 'bold';
            }
            else
            {
            cvalD.style.backgroundColor = '';
            cvalD.style.border = '';
            cvalD.style.fontWeight = '';
            }
                
            if(!isMvalid)
            {
            //alert("M");
            cvalM.style.backgroundColor = '#fcc';
            cvalM.style.border = 'Solid 1px #f00';
            cvalM.style.fontWeight = 'bold';
            }
            else
            {
            cvalM.style.backgroundColor = '';
            cvalM.style.border = '';
            cvalM.style.fontWeight = '';
            }
                
            if(!isYvalid)
            {
            //alert("Y");
            cvalY.style.backgroundColor = '#fcc';
            cvalY.style.border = 'Solid 1px #f00';
            cvalY.style.fontWeight = 'bold';
            }
            else
            {
            cvalY.style.backgroundColor = '';
            cvalY.style.border = '';
            cvalY.style.fontWeight = '';
            }
            */
        }
        else {
            cvalD.style.backgroundColor = '';
            cvalD.style.border = '';
            cvalD.style.fontWeight = '';

            cvalM.style.backgroundColor = '';
            cvalM.style.border = '';
            cvalM.style.fontWeight = '';

            cvalY.style.backgroundColor = '';
            cvalY.style.border = '';
            cvalY.style.fontWeight = '';
        }
    }
    args.IsValid = isvalid;
    return;
}

function IsEnableDateValidation(prefix, onOff) {
    ValidatorEnable(document.getElementById(prefix + "_rfvDay"), onOff);
    ValidatorEnable(document.getElementById(prefix + "_rfvMonth"), onOff)
    ValidatorEnable(document.getElementById(prefix + "_rfvYear"), onOff);
    ValidatorEnable(document.getElementById(prefix + "_csvDate"), onOff);
}
    
   
   
